<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once ('pagination_info.php');

class Billing extends CI_Controller {

	function __construct() {	        
	    parent::__construct();
	    $this->load->helper('url');
	    $this->load->helper('form');
             $this->load->library('pagination');
            $this->load->model('Billing_Model', "billing");
             $this->page_info = new CPagination_details();
	}
	public function index() {
	    $data['standard'] = array("--Select--", "LKG", "UKG", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII");
	    $base_url = site_url('index.php/billing/index');
            $searchBillNo = trim($this->input->post('searchBillNo'));
            $searchStudentName = trim($this->input->post('searchStudentName'));
            $searchStandard = trim($this->input->post('searchStandard'));
	    $searchBillDate = trim($this->input->post('searchBillDate'));
	    $cur_page = trim($this->input->post('page'));     
	    $filter_sql='';
		if (strlen($searchBillNo) > 0) {
			$filter_sql .= (strlen($filter_sql)>0 ? ' and ' : '');
			$filter_sql .= " bill_no = '".$searchBillNo."'";
		}
				
		if (strlen($searchStudentName) > 0) {
			$filter_sql .= (strlen($filter_sql)>0 ? ' and ' : '');
			$filter_sql .= " student_name = '".$searchStudentName."'";
		}

		if (strlen($searchStandard) > 0 && $searchStandard != "--Select--") {
			$filter_sql .= (strlen($filter_sql) >0 ? ' and ' : '');
			$filter_sql .= " standard = '".$searchStandard . "'";
		}
		if (strlen($searchBillDate) > 0) {
			$filter_sql .= (strlen($filter_sql)>0 ? ' and ' : '');
			$filter_sql .= " bill_date = '".$searchBillDate."'";
		}


	    $rows =    $this->billing->getBillList($filter_sql, null);
	    $config = $this->page_info->config_pagination_initialize(count($rows), $base_url);
	    $this->pagination->initialize($config);
	    $this->page_info->offset = $cur_page;
	    $this->page_info->limit = $config['per_page'];
	    $data['bill_details'] = $this->billing->getBillList($filter_sql, $this->page_info);
	    $data['pagination'] = $this->pagination->create_links($cur_page);
	    $data['cur_page'] = $cur_page;
	    $this->load->view('view_billing', $data);
	}
	public function savePrintBill() {
            $studentName  = $this->input->post('studentName');
	    $standard  = $this->input->post('standard');
	    $registrationRs = $this->input->post('registrationRs');
	    $registrationP = $this->input->post('registrationP');
	    $admissionRs = $this->input->post('admissionRs');
	    $admissionP = $this->input->post('admissionP');
	    $tuitionRs = $this->input->post('tuitionRs');
	    $tuitionP = $this->input->post('tuitionP');
	    $LaboratoryRs =  $this->input->post('LaboratoryRs');
	    $LaboratoryP = $this->input->post('LaboratoryP');
	    $libraryRs = $this->input->post('libraryRs');
	    $libraryP = $this->input->post('libraryP');
	    $gamesRs = $this->input->post('gamesRs');
	    $gamesP = $this->input->post('gamesP');
	    $specialRs = $this->input->post('specialRs');
	    $specialP = $this->input->post('specialP');
	    $computerRs = $this->input->post('computerRs');
	    $computerP = $this->input->post('computerP');
	    $stationaryRs = $this->input->post('stationaryRs');
	    $stationaryP = $this->input->post('stationaryP');
	    $specialAmenitiesRs = $this->input->post('specialAmenitiesRs');
	    $specialAmenitiesP = $this->input->post('specialAmenitiesP');
	    $transferCertificateRs = $this->input->post('transferCertificateRs');
	    $transferCertificateP = $this->input->post('transferCertificateP');
	    $hobbyRs = $this->input->post('hobbyRs');
	    $hobbyP = $this->input->post('hobbyP');
	    $smartClassFeeRs = $this->input->post('smartClassFeeRs');
	    $smartClassFeeP = $this->input->post('smartClassFeeP');
	    $fineRs = $this->input->post('fineRs');
	    $fineP = $this->input->post('fineP');
	    $receiptRs = $this->input->post('receiptRs');
	    $receiptP = $this->input->post('receiptP');
	    $chequeNo = $this->input->post('chequeNo');
	    $chequeBankName = $this->input->post('chequeBankName');
	    $totalRs = $this->input->post('totalRs');

	    $registration = floatval($registrationRs) + floatval($registrationP);
            $admission = floatval($admissionRs) + floatval($admissionP);
            $tuition = floatval($tuitionRs) + floatval($tuitionP);
            $Laboratory = floatval($LaboratoryRs) + floatval($LaboratoryP);
            $library = floatval($libraryRs) + floatval($libraryP);
            $games = floatval($gamesRs) + floatval($gamesP);
	    $special = floatval($specialRs) + floatval($specialP);
            $computer = floatval($computerRs) + floatval($computerP);
            $stationary = floatval($stationaryRs) + floatval($stationaryP);
            $specialAmenities = floatval($specialAmenitiesRs) + floatval($specialAmenitiesP);
            $transferCertificate = floatval($transferCertificateRs) + floatval($transferCertificateP);
            $smartClassFee = floatval($smartClassFeeRs) + floatval($smartClassFeeP);
            $hobby = floatval($hobbyRs) + floatval($hobbyP);
            $fine = floatval($fineRs) + floatval($fineP);
            $receipt = floatval($receiptRs) + floatval($receiptP);
            $totalRs = floatval($totalRs);
             $bill_no_suffix = "1" ;
	    $last_bill_no = $this->billing->get_last_bill_no();

            if (!empty($last_bill_no)) {
		$bill_no_suffix = intval(substr($last_bill_no[0]['bill_no'], 4)) + 1;
	    }
            $bill_no =  date('m').date('y').$bill_no_suffix;
            date_default_timezone_set('UTC');
	    $data =  array( 'student_name' => $studentName,
                            'bill_no'      => $bill_no,
			    'standard' => $standard,
			    'registration_fee' => $registration ,
			    'admission_fee' => $admission,
			    'tuition_fee' => $tuition,
			    'laboratory_fee' => $Laboratory,
			    'library_fee' =>  $library,
			    'sports_fee' => $games,
			    'special_fee' => $special,
			    'computer_fee' => $computer,
			    'stationary_fee' => $stationary,
			    'special_amenities_fee' => $specialAmenities,
			    'transfer_certificate_fee' =>  $transferCertificate,
			    'hobby_fee' => $hobby,
			    'smart_class_fee' => $smartClassFee,
			    'fine' => $fine,
			    'misc_receipt' => $receipt,
			    'cheque_no' => $chequeNo,
			    'bank_name' => $chequeBankName,
			    'bill_date' => date('Y-m-d H:i:s'),
			    'is_bill_printed' => 0,
			    'total' => $totalRs
		    );
	    $bill_id = $this->billing->insert_bill_entry($data);
	    $this->printBill($bill_id);
	}


        public function saveBill() {
            $studentName  = $this->input->post('studentName');
	    $standard  = $this->input->post('standard');
	    $registrationRs = $this->input->post('registrationRs');
	    $registrationP = $this->input->post('registrationP');
	    $admissionRs = $this->input->post('admissionRs');
	    $admissionP = $this->input->post('admissionP');
	    $tuitionRs = $this->input->post('tuitionRs');
	    $tuitionP = $this->input->post('tuitionP');
	    $LaboratoryRs =  $this->input->post('LaboratoryRs');
	    $LaboratoryP = $this->input->post('LaboratoryP');
	    $libraryRs = $this->input->post('libraryRs');
	    $libraryP = $this->input->post('libraryP');
	    $gamesRs = $this->input->post('gamesRs');
	    $gamesP = $this->input->post('gamesP');
	    $specialRs = $this->input->post('specialRs');
	    $specialP = $this->input->post('specialP');
	    $computerRs = $this->input->post('computerRs');
	    $computerP = $this->input->post('computerP');
	    $stationaryRs = $this->input->post('stationaryRs');
	    $stationaryP = $this->input->post('stationaryP');
	    $specialAmenitiesRs = $this->input->post('specialAmenitiesRs');
	    $specialAmenitiesP = $this->input->post('specialAmenitiesP');
	    $transferCertificateRs = $this->input->post('transferCertificateRs');
	    $transferCertificateP = $this->input->post('transferCertificateP');
	    $hobbyRs = $this->input->post('hobbyRs');
	    $hobbyP = $this->input->post('hobbyP');
	    $smartClassFeeRs = $this->input->post('smartClassFeeRs');
	    $smartClassFeeP = $this->input->post('smartClassFeeP');
	    $fineRs = $this->input->post('fineRs');
	    $fineP = $this->input->post('fineP');
	    $receiptRs = $this->input->post('receiptRs');
	    $receiptP = $this->input->post('receiptP');
	    $chequeNo = $this->input->post('chequeNo');
	    $chequeBankName = $this->input->post('chequeBankName');
	    $totalRs = $this->input->post('totalRs');

	    $registration = floatval($registrationRs) + floatval($registrationP);
            $admission = floatval($admissionRs) + floatval($admissionP);
            $tuition = floatval($tuitionRs) + floatval($tuitionP);
            $Laboratory = floatval($LaboratoryRs) + floatval($LaboratoryP);
            $library = floatval($libraryRs) + floatval($libraryP);
            $games = floatval($gamesRs) + floatval($gamesP);
	    $special = floatval($specialRs) + floatval($specialP);
            $computer = floatval($computerRs) + floatval($computerP);
            $stationary = floatval($stationaryRs) + floatval($stationaryP);
            $specialAmenities = floatval($specialAmenitiesRs) + floatval($specialAmenitiesP);
            $transferCertificate = floatval($transferCertificateRs) + floatval($transferCertificateP);
            $smartClassFee = floatval($smartClassFeeRs) + floatval($smartClassFeeP);
            $hobby = floatval($hobbyRs) + floatval($hobbyP);
            $fine = floatval($fineRs) + floatval($fineP);
            $receipt = floatval($receiptRs) + floatval($receiptP);
            $totalRs = floatval($totalRs);
             $bill_no_suffix = "1" ;
	    $last_bill_no = $this->billing->get_last_bill_no();

            if (!empty($last_bill_no)) {
		$bill_no_suffix = intval(substr($last_bill_no[0]['bill_no'], 4)) + 1;
	    }
            $bill_no =  date('m').date('y').$bill_no_suffix;
            date_default_timezone_set('UTC');
	    $data =  array( 'student_name' => $studentName,
                            'bill_no'      => $bill_no,
			    'standard' => $standard,
			    'registration_fee' => $registration ,
			    'admission_fee' => $admission,
			    'tuition_fee' => $tuition,
			    'laboratory_fee' => $Laboratory,
			    'library_fee' =>  $library,
			    'sports_fee' => $games,
			    'special_fee' => $special,
			    'computer_fee' => $computer,
			    'stationary_fee' => $stationary,
			    'special_amenities_fee' => $specialAmenities,
			    'transfer_certificate_fee' =>  $transferCertificate,
			    'hobby_fee' => $hobby,
			    'smart_class_fee' => $smartClassFee,
			    'fine' => $fine,
			    'misc_receipt' => $receipt,
			    'cheque_no' => $chequeNo,
			    'bank_name' => $chequeBankName,
			    'bill_date' => date('Y-m-d H:i:s'),
			    'is_bill_printed' => 0,
			    'total' => $totalRs
		    );
	    $bill_id = $this->billing->insert_bill_entry($data);
	    redirect();
	}

        function lpr($STR,$PRN) {
	    $prn=(isset($PRN) && strlen($PRN))?"$PRN": "tvs" ;
	    $CMDLINE="lpr -P $prn ";
	    $pipe=popen("$CMDLINE" , 'w' );
	    if (!$pipe) {print "pipe failed."; return ""; }
	    fputs($pipe,$STR);
	    pclose($pipe);
	}

	function printBill($bill_id) { 
	    $bill_details = $this->billing->getBillById($bill_id);
	    if ( empty($bill_details))
            return;
	      $bill_detail = $bill_details[0];
	      $space = ' ';
	      $pad_char = " ";
	      $text = '';
	      $text .= PHP_EOL;
	      $text .= str_pad('velammal engineering college', 36, $pad_char, STR_PAD_BOTH);
	      $text .= PHP_EOL;
	      $text .= str_pad('R S ROAD, CHENNAI 21', 36, $pad_char, STR_PAD_BOTH);
	      $text .= PHP_EOL;
	      $text .= PHP_EOL;
	      $text .= PHP_EOL;
	      $text .= 'Bill No: '.$bill_detail['bill_no'].'             '.date("d/m/Y", strtotime($bill_detail['bill_date']));
	      $text .= PHP_EOL;
	      $text .= $bill_detail['student_name'];
	      $text .= ', '.$bill_detail['standard'];
	      $text .= PHP_EOL;
	      $text .= str_repeat('-', 36);
	      $text .= PHP_EOL;
	      $text .= str_pad('Fee Particulars ', 30);
	      $text .= str_pad('Amount', 6, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;
	      $text .= str_repeat('-', 36);
	      $text .= PHP_EOL;
	      $amount = 0.00;
	      $sno = 1;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Registration Fee", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['registration_fee']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;
	    
	      $sno = 2;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Admission Fee", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['admission_fee']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;

	      $sno = 3;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Tuition Fee", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['tuition_fee']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;

	      $sno = 4;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Laboratory Fee", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['laboratory_fee']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;

	      $sno = 5;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Library Fee", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['library_fee']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;

	      $sno = 6;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Sports Fee", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['sports_fee']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;

	      $sno = 7;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Special Fee", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['special_fee']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;

	      $sno = 8;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Computer Fee", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['computer_fee']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;

	      $sno = 9;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Stationary Fee", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['stationary_fee']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;

	      $sno = 10;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Special Amenities Fee", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['special_amenities_fee']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;

	      $sno = 11;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Transfer Certificate Fee", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['transfer_certificate_fee']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;

	      $sno = 12;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Hobby Fee", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['hobby_fee']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;

	      $sno = 13;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Smart Class Fee", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['smart_class_fee']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;

	      $sno = 14;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Fine", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['fine']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;

	      $sno = 15;
	      $str = $this->right("0" . $sno, 2, "0");
	      $text .= str_pad($str, 3, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Misc Receipt", 26, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($this->number($bill_detail['misc_receipt']), 7, $pad_char, STR_PAD_LEFT);
	      $text .= PHP_EOL;
	      $text .= str_repeat('-', 36);
	      $text .= PHP_EOL;
	      
	      $text .= str_pad("* By cheque No:", 15, $pad_char, STR_PAD_RIGHT);	
	      if (!empty($bill_detail['cheque_no'])) { 
		$text .= str_pad($bill_detail['cheque_no'], 10, $pad_char, STR_PAD_RIGHT);
	      } else { 
		$text .= str_pad(".............", 10, $pad_char, STR_PAD_RIGHT);
	      }

	      $text .= PHP_EOL;
	      $text .= str_pad("          Name:", 15, $pad_char, STR_PAD_RIGHT);	
	      if (!empty($bill_detail['bank_name'])) { 
		$text .= str_pad($bill_detail['bank_name'], 10, $pad_char, STR_PAD_RIGHT);
	      } else { 
		$text .= str_pad(".............", 10, $pad_char, STR_PAD_RIGHT);
	      }

	      $text .= PHP_EOL;
	      $text .= str_pad("By Cash                ", 15, $pad_char, STR_PAD_RIGHT);	
	      $text .= str_pad("Total", 10, $pad_char, STR_PAD_RIGHT);
	      $text .= str_pad($bill_detail["total"], 11, $pad_char, STR_PAD_RIGHT);

	      $text .= PHP_EOL;
	      $text .= str_repeat('-', 36);
	      $text .= PHP_EOL;
	      $text .= str_pad("* cheques are subject to realisation", 36, $pad_char, STR_PAD_RIGHT);	
	      $text .= PHP_EOL;
	      $text .= PHP_EOL;
	      $text .= str_pad("                       For PRINCIPAL", 36, $pad_char, STR_PAD_RIGHT);	


	      $reportData = "<pre>" . $text . "</pre>";
	      $this->lpr($reportData, "tvs");
	      redirect();
	}

	function right($value, $count){ 
	  return substr($value, ($count*-1)); 
	}
  
	function number($num)	{ 
	    return ((int) $num); 
	}
}


